import Vue from 'vue'
import App from './App.vue'
import store from './store'
import router from './router'
import { getToken, setToken } from '@/utils/auth'
import { login, getInfo } from '@/api/auth'
import fastClick from 'fastclick'
import Vant from 'vant';
import { Lazyload } from 'vant'
import 'vant/lib/index.css'


import './assets/css/reset.css'
import './assets/css/border.css'

fastClick.attach(document.body)
Vue.config.productionTip = false
Vue.use(Vant)
Vue.use(Lazyload)

router.beforeEach((to, from, next) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if(to.query.code) {
  	getInfo(to.query).then(res=>{
      setToken(res.access_token)
      store.commit('SET_USER', res.user)
      console.log(to.path)
      window.location.href = to.path
    })
  }
  let token = getToken()
  if(!token) {
  	login({redirect: window.location.href}).then(res=>{
      window.location.href = res
    })
  }else{
  	next()
  }
})
new Vue({
  store,
  router,
  render: h => h(App)
}).$mount('#app')
